home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / Target Acquisition Manager / Source / source / CAppearanceApp.cp < prev    next >
Encoding:
Text File  |  1999-06-25  |  8.4 KB  |  332 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CAppearanceApp.cp         ©1994-1998 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //    This file contains the starter code for an Appearance PowerPlant application
  5.  
  6. #include "CAppearanceApp.h"
  7.  
  8. #include <LGrowZone.h>
  9. #include <PP_Messages.h>
  10. #include <PP_Resources.h>
  11. #include <PPobClasses.h>
  12. #include <UDrawingState.h>
  13. #include <UMemoryMgr.h>
  14. #include <URegistrar.h>
  15.  
  16. #include <LWindow.h>
  17. #include <LCaption.h>
  18.  
  19. #include <UControlRegistry.h>
  20. #include <UGraphicUtils.h>
  21. #include <UEnvironment.h>
  22.  
  23. #include <Appearance.h>
  24. #include <LDialogBox.h>
  25. #include <LPushButton.h>
  26. #include <LPictureControl.h>
  27. #include "LClipPicture.h"
  28. #include "FinderFun.h"
  29. #include <LStaticText.h>
  30.  
  31. // put declarations for resource ids (ResIDTs) here
  32.  
  33. const PP_PowerPlant::ResIDT    wind_SampleWindow     = 128;        // EXAMPLE, create a new window
  34. const PP_PowerPlant::ResIDT    wind_PasswordDialog = 129;        // EXAMPLE, create a new window
  35.  
  36. const MessageT    cmd_Password                    = 2000;        // nil
  37.  
  38.  
  39. static PP_PowerPlant::LWindow*     sPasswordDialog = nil;
  40. static PP_PowerPlant::LWindow*     sMainWindow = nil;
  41.  
  42. static UInt32                    sCount = 0;
  43. static UInt32                    sIndex = 0;
  44. static UInt32                    sState = 0;
  45. static Point                    sPoints[6];
  46.  
  47.  
  48. extern "C" void AquireTarget(PicHandle inPic, Point* outPoints, UInt32* outCount);
  49.  
  50.  
  51. // ===========================================================================
  52. //        • Main Program
  53. // ===========================================================================
  54.  
  55. int main()
  56. {
  57.                                 
  58.     SetDebugThrow_(PP_PowerPlant::debugAction_Alert);    // Set Debugging options
  59.     SetDebugSignal_(PP_PowerPlant::debugAction_Alert);
  60.  
  61.     PP_PowerPlant::InitializeHeap(3);                    // Initialize Memory Manager
  62.                                                         // Parameter is number of Master Pointer
  63.                                                         // blocks to allocate
  64.     
  65.     PP_PowerPlant::UQDGlobals::InitializeToolbox(&qd);    // Initialize standard Toolbox managers
  66.     
  67.     new PP_PowerPlant::LGrowZone(20000);                // Install a GrowZone function to catch
  68.                                                         // low memory situations.
  69.  
  70.     CAppearanceApp    theApp;                                // create instance of your application
  71.     theApp.Run();
  72.     
  73.     return 0;
  74. }
  75.  
  76.  
  77. // ---------------------------------------------------------------------------
  78. //        • CAppearanceApp
  79. // ---------------------------------------------------------------------------
  80. //    Constructor
  81.  
  82. CAppearanceApp::CAppearanceApp()
  83. {
  84.     if ( PP_PowerPlant::UEnvironment::HasFeature( PP_PowerPlant::env_HasAppearance ) ) {
  85.         ::RegisterAppearanceClient();
  86.     }
  87.  
  88.     RegisterClass_(PP_PowerPlant::LWindow);        // You must register each kind of
  89.     RegisterClass_(PP_PowerPlant::LCaption);    // PowerPlant classes that you use
  90.                                                 // in your PPob resource.
  91.     RegisterClass_(PP_PowerPlant::LDialogBox);
  92.     RegisterClass_(PP_PowerPlant::LTabGroup);
  93.     RegisterClass_(PP_PowerPlant::LTabGroupView);
  94.     RegisterClass_(PP_PowerPlant::LClipPicture);
  95.     
  96.     // Register the Appearance Manager/GA classes
  97.     PP_PowerPlant::UControlRegistry::RegisterClasses();
  98. }
  99.  
  100.  
  101. // ---------------------------------------------------------------------------
  102. //        • ~CAppearanceApp
  103. // ---------------------------------------------------------------------------
  104. //    Destructor
  105. //
  106.  
  107. CAppearanceApp::~CAppearanceApp()
  108. {
  109. }
  110.  
  111. // ---------------------------------------------------------------------------
  112. //        • StartUp
  113. // ---------------------------------------------------------------------------
  114. //    This method lets you do something when the application starts up
  115. //    without a document. For example, you could issue your own new command.
  116.  
  117. void
  118. CAppearanceApp::StartUp()
  119. {
  120.     ObeyCommand(PP_PowerPlant::cmd_Password, nil);    // EXAMPLE, create a new window
  121.     
  122.     StartIdling();
  123. }
  124.  
  125. // ---------------------------------------------------------------------------
  126. //        • SpendTime
  127. // ---------------------------------------------------------------------------
  128.  
  129. void
  130. CAppearanceApp::SpendTime(const EventRecord& /* inMacEvent */)
  131. {
  132.     //    Click on the icon
  133.     if ((sCount > 0) && (sState == 0))
  134.     {
  135.         ClickOnIcon(sPoints[sIndex]);
  136.         sIndex++;
  137.     }
  138.     
  139.     //    Bring me back to the front
  140.     if ((sCount > 0) && (sState > 30))
  141.     {
  142.         ProcessSerialNumber        psn;
  143.         
  144.         MacGetCurrentProcess(&psn);
  145.         
  146.         SetFrontProcess(&psn);
  147.     }
  148.     
  149.     //    Move it to the trash
  150.     if ((sCount > 0) && (sState > 60))
  151.     {
  152.         MoveFileToTrash();
  153.         
  154.     }
  155.     
  156.     //    This needs to be above "Next"
  157.     sState++;
  158.  
  159.     //    Next
  160.     if ((sCount > 0) && (sState > 90))
  161.     {
  162.         sCount--;
  163.         sState = 0;
  164.     }
  165. }
  166.  
  167. // ---------------------------------------------------------------------------
  168. //        • ListenToMessage
  169. // ---------------------------------------------------------------------------
  170.  
  171. void
  172. CAppearanceApp::ListenToMessage(MessageT inMessage, void* /* ioParam */)
  173. {
  174.     switch (inMessage)
  175.     {
  176.         case PP_PowerPlant::msg_OK:
  177.             sPasswordDialog->DoClose();
  178.             sPasswordDialog = nil;
  179.             ObeyCommand(PP_PowerPlant::cmd_New, nil);
  180.             break;
  181.         
  182.         case PP_PowerPlant::msg_Cancel:
  183.             SysBeep(10);
  184.             break;
  185.         
  186.         case 'Seek':
  187.             if (sMainWindow != nil)
  188.             {
  189.                 LClipPicture* thePicture = (LClipPicture*)sMainWindow->FindPaneByID(256);
  190.                 ThrowIfNil_(thePicture);
  191.                 
  192.                 PicHandle    thePict = thePicture->GetPicture();
  193.                 
  194.                 if (thePict != nil)
  195.                 {
  196.                     sCount = 0;
  197.                     sState = 0;
  198.                     sIndex = 0;
  199.                     
  200.                     //    Lets go hunting!
  201.                     AquireTarget(thePict, sPoints, &sCount);
  202.             
  203.                 }
  204.             }
  205.             break;
  206.         
  207.         default:
  208.             break;
  209.     };
  210. }
  211.  
  212. // ---------------------------------------------------------------------------
  213. //        • ObeyCommand
  214. // ---------------------------------------------------------------------------
  215. //    This method lets the application respond to commands like Menu commands
  216.  
  217. Boolean
  218. CAppearanceApp::ObeyCommand(
  219.     PP_PowerPlant::CommandT    inCommand,
  220.     void                    *ioParam)
  221. {
  222.     Boolean                cmdHandled = true;
  223.     LPushButton*         theButton;
  224.     
  225.     switch (inCommand) {
  226.     
  227.         // Handle command messages (defined in PP_Messages.h).
  228.         case PP_PowerPlant::cmd_Password:
  229.             //    Create a dialog to get the user's password
  230.             sPasswordDialog = PP_PowerPlant::LWindow::CreateWindow(wind_PasswordDialog, this);
  231.             ThrowIfNil_(sPasswordDialog);
  232.             
  233.             //    Find the button
  234.             theButton = (LPushButton*)sPasswordDialog->FindPaneByID(128);
  235.             ThrowIfNil_(theButton);
  236.             
  237.             //    This is a little weird, we hook up the app as the listener
  238.             theButton->AddListener(this);
  239.             break;
  240.  
  241.         case PP_PowerPlant::cmd_New:
  242.             //    Create a dialog to get the user's password
  243.             sMainWindow = PP_PowerPlant::LWindow::CreateWindow(wind_SampleWindow, this);
  244.             ThrowIfNil_(sMainWindow);
  245.  
  246.             //    Find the button
  247.             theButton = (LPushButton*)sMainWindow->FindPaneByID(300);
  248.             ThrowIfNil_(theButton);
  249.             
  250.             //    This is a little weird, we hook up the app as the listener
  251.             theButton->AddListener(this);
  252.             break;
  253.             
  254.         case PP_PowerPlant::cmd_Paste:
  255.             if (sMainWindow != nil)
  256.             {
  257.                 Handle    tempScrap = NewHandle(0);
  258.                 SInt32    offset = 0;
  259.                 
  260.                 LClipPicture* thePicture = (LClipPicture*)sMainWindow->FindPaneByID(256);
  261.                 ThrowIfNil_(thePicture);
  262.                 
  263.                 LoadScrap();
  264.                 
  265.                 GetScrap(tempScrap, 'PICT', &offset);
  266.                 
  267.                 if (GetHandleSize(tempScrap) != 0)
  268.                     thePicture->SetPicture((PicHandle)tempScrap);
  269.                 
  270.                 LStaticText* hiddenText = (LStaticText*)sMainWindow->FindPaneByID(400);
  271.                 ThrowIfNil_(hiddenText);
  272.                 
  273.                 hiddenText->Show();
  274.                 
  275.                 hiddenText = (LStaticText*)sMainWindow->FindPaneByID(401);
  276.                 ThrowIfNil_(hiddenText);
  277.                 
  278.                 hiddenText->Show();
  279.             }
  280.             break;
  281.  
  282.         // Any that you don't handle, such as cmd_About and cmd_Quit,
  283.         // will be passed up to LApplication
  284.         default:
  285.             cmdHandled = PP_PowerPlant::LApplication::ObeyCommand(inCommand, ioParam);
  286.             break;
  287.     }
  288.     
  289.     return cmdHandled;
  290. }
  291.  
  292. // ---------------------------------------------------------------------------
  293. //        • FindCommandStatus
  294. // ---------------------------------------------------------------------------
  295. //    This function enables menu commands.
  296. //
  297.  
  298. void
  299. CAppearanceApp::FindCommandStatus(
  300.     PP_PowerPlant::CommandT    inCommand,
  301.     Boolean                    &outEnabled,
  302.     Boolean                    &outUsesMark,
  303.     PP_PowerPlant::Char16    &outMark,
  304.     Str255                    outName)
  305. {
  306.  
  307.     switch (inCommand) {
  308.     
  309.         case PP_PowerPlant::cmd_New:
  310.             if (sPasswordDialog == nil)
  311.                 outEnabled = true;
  312.             break;
  313.             
  314.         case PP_PowerPlant::cmd_Paste:
  315.             if (sMainWindow != nil)
  316.                 outEnabled = true;
  317.             break;
  318.  
  319.         // Return menu item status according to command messages.
  320.         case PP_PowerPlant::cmd_Password:
  321.             outEnabled = true;
  322.             break;
  323.         
  324.         // Any that you don't handle, such as cmd_About and cmd_Quit,
  325.         // will be passed up to LApplication
  326.         default:
  327.             PP_PowerPlant::LApplication::FindCommandStatus(inCommand, outEnabled,
  328.                                                 outUsesMark, outMark, outName);
  329.             break;
  330.     }
  331. }
  332.